package com.yuneec.fly.dialogs;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;
import android.widget.SeekBar;
import android.widget.TextView;

import com.yuneec.fly.R;

/**
 * Created by jw.gu on 2016/6/25.
 */
@SuppressLint("ValidFragment")
public class ConfirmDialog extends BaseDialogFragment {
    private OnCancelListener onCancelListener;
    private OnConfirmListener onConfirmListener;
    private String message;


    public ConfirmDialog(String message, OnConfirmListener onConfirmListener, OnCancelListener onCancelListener) {
        super();
        this.onConfirmListener = onConfirmListener;
        this.onCancelListener = onCancelListener;
        this.message = message;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCancelable(false);
        View view = inflater.inflate(R.layout.confirm_with_bar, container);
        view.findViewById(R.id.confirm_dialog_radioGroup).setVisibility(View.GONE);

        ((TextView) view.findViewById(R.id.dialogMessage)).setText(message);

        ((SeekBar) view.findViewById(R.id.confirmSB)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress == seekBar.getMax()) {
                    if (onConfirmListener != null) {
                        onConfirmListener.onConfirm(ConfirmDialog.this);
                    } else {
                        dismiss();
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(final SeekBar seekBar) {
                if (seekBar.getProgress() < seekBar.getMax()) {
                    int[] count = {seekBar.getProgress(), 0};
                    ObjectAnimator animator = ObjectAnimator.ofInt(new Object(), "", count);
                    animator.setDuration(200);
                    animator.setInterpolator(new AccelerateInterpolator(1));
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            if (seekBar != null) {
                                seekBar.setProgress((Integer) animation.getAnimatedValue());
                            }
                        }
                    });
                    animator.start();
                }
            }
        });
        (view.findViewById(R.id.cancelIV)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCancelListener != null) {
                    onCancelListener.onCancel(ConfirmDialog.this);
                } else {
                    dismiss();
                }
            }
        });
        return view;
    }

}
